#include "OrderDataAccess.h"

// 显示所有订单
QList<Order> OrdersDataAccess::getOrders(const int &userId)
{
    DbConnector dbConnector;
    // QSqlDatabase tempDb = dbConnector.getDatabase();
    // QSqlQuery query(tempDb);
    QSqlQuery query(dbConnector.getDatabase());

    QString sql = "SELECT order_id, order_date, total_amount, name, phone, address FROM Orders WHERE user_id = ?";
    query.prepare(sql);
    query.addBindValue(userId);
    if (!query.exec())
    {
        qDebug() << "查询订单信息失败：";
        qDebug() << "userId：" << userId;
        qDebug() << "Executed SQL:" << query.lastQuery();
        return QList<Order>(); // 返回空列表
    }

    QList<Order> orders;

    while (query.next())
    {
        Order order;

        // 设置 Orders 类的属性
        order.orderId = query.value("order_id").toInt();
        order.orderDate = query.value("order_date").toDateTime();
        order.totalAmount = query.value("total_amount").toDouble();
        order.name = query.value("name").toString();
        order.phone = query.value("phone").toString();
        order.address = query.value("address").toString();

        orders.append(order);
    }
    return orders;
}

// 显示订单详情 getOrderDetails
QList<OrderDetail> OrdersDataAccess::getOrderDetails(const int &orderId)
{
    DbConnector dbConnector;
    QSqlQuery query(dbConnector.getDatabase());

    QString sql = "SELECT product_id, quantity, price FROM OrderDetails WHERE order_id = ?";
    query.prepare(sql);
    query.addBindValue(orderId);
    if (!query.exec())
    {
        qDebug() << "查询订单详情失败：";
        return QList<OrderDetail>(); // 返回空列表
    }

    QList<OrderDetail> orderDetails;

    while (query.next())
    {
        OrderDetail orderDetail;

        // 设置 OrderDetails 类的属性
        orderDetail.productId = query.value("Product_ID").toInt();
        orderDetail.quantity = query.value("Quantity").toInt();
        orderDetail.price = query.value("Price").toDouble();

        orderDetails.append(orderDetail);
    }

    return orderDetails;
}

// 添加订单功能 creatOrder
int OrdersDataAccess::creatOrder(const int &userId, const QDateTime &orderDate, const double &totalAmount, const QString &name, const QString &phone, const QString &address)
{
    DbConnector dbConnector;
    QSqlQuery query(dbConnector.getDatabase());

    // 准备 SQL 插入语句
    QString sql = "INSERT INTO Orders (user_ID, Order_Date, total_Amount, name, phone, address) VALUES (?, ?, ?, ?, ?, ?)";
    query.prepare(sql);

    // 绑定参数
    query.addBindValue(userId);
    query.addBindValue(orderDate);
    query.addBindValue(totalAmount);
    query.addBindValue(name);
    query.addBindValue(phone);
    query.addBindValue(address);

    // 执行 SQL 语句
    if (!query.exec())
    {
        qDebug() << "插入订单信息失败：";
        return -1;
    }

    // 获取最后插入的订单号
    if (!query.exec("SELECT LAST_INSERT_ID()"))
    {
        qDebug() << "无法获取最后插入的订单号：";
        return -1; // 返回 -1 表示错误
    }

    if (query.next())
    {
        int orderId = query.value(0).toInt();
        return orderId; // 返回订单号
    }

    return -1;
}

// 添加订单详情 addOrderDetail
bool OrdersDataAccess::addOrderDetail(const int &orderId, const int &productId, const int &quantity, const int &price)
{
    DbConnector dbConnector;
    QSqlQuery query(dbConnector.getDatabase());

    // 准备 SQL 插入语句
    QString sql = "INSERT INTO OrderDetails (order_Id, product_Id, quantity, price) VALUES (?, ?, ?, ?)";
    query.prepare(sql);

    // 绑定参数
    query.addBindValue(orderId);
    query.addBindValue(productId);
    query.addBindValue(quantity);
    query.addBindValue(price);
    if (!query.exec())
    {
        qDebug() << "添加订单详情失败";
        return false;
    }
    return true;
}
